package com.woniuxy.rbac.filter;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @Auther: 老谭（<a href="http://www.woniuxy.com">蜗牛学苑</a>）
 */
public class MemberFilter implements Filter {
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) request;
        HttpServletResponse resp = (HttpServletResponse) response;
        HttpSession session = req.getSession();
        if (session.getAttribute("member") == null) { //未登录
            String header = req.getHeader("");
            if(header==null) {
                resp.sendRedirect("/member/login");
                return;
            }else{
                PrintWriter writer=resp.getWriter();
                writer.write("");
                writer.close();
                return;
            }
        }
        chain.doFilter(req, resp);
    }
}
